import create from '../libs/create.js'
import { AppPage } from './AppPage.js'
import { globalStore, homeStore, PagesStore } from './store'

export type StoreDataType<T extends keyof PageOptions> = {
  [k in T]: { [key: string]: any }
}

type StoreData<N extends keyof PageOptions> = Pick<PagesStore, N> & typeof globalStore.data

export interface Store<N extends keyof PageOptions> {
  data: StoreData<N>
  update: (data?: StoreData<N>) => Promise<any>
}

class Westore {

  private storeObj: Partial<Store<keyof PageOptions>> = {
    data: {
      home: homeStore,
      me: {},
      square: {},
      ...globalStore.data
    }
  }

  public create(name: keyof PageOptions, pageOptions: PageInitData<AppPage<any, any>['data']>) {
    create(this.storeObj, pageOptions)
  }

  public getStoreData(name: keyof PageOptions) {
    return this.storeObj.data![name]
  }
}

export default new Westore()
